import 'package:dotted_line/dotted_line.dart';
import 'package:flutter_menu/biz/order/detail/option/options_controller.dart';
import 'package:flutter_menu/biz/order/detail/package/package_controller.dart';
import 'package:flutter_menu/biz/order/widget/count_number_button.dart';
import 'package:flutter_menu/biz/order/widget/option_selector.dart';
import 'package:flutter_menu/res/app_design.dart';
import 'package:get/get.dart';

class OptionsPackageDialog extends GetView<OptionsController> {
  const OptionsPackageDialog(this.data, {super.key});

  final CombineFoodWithSpec data;

  @override
  Widget build(BuildContext context) {
    var widthOptionDialog = 722.w;
    var heightOptionDialog = 831.h;
    return GetBuilder<OptionsController>(
        init: OptionsController(),
        builder: (controller) {
          debugPrint("OptionsPackageDialog Build");
          return Container(
            padding: EdgeInsets.symmetric(vertical: 30.w, horizontal: 26.w),
            width: widthOptionDialog,
            height: heightOptionDialog,
            child: Column(
              mainAxisSize: MainAxisSize.max,
              children: [
                Column(
                  mainAxisSize: MainAxisSize.min,
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    // 标题
                    Text(data.name ?? "",
                        style: TextStyle(
                            fontSize: 35.sp,
                            color: AppColors.textInBottomSheet)),
                    SizedBox(height: 45.w),

                    // 数量选择器
                    const DottedLine(
                      dashColor: AppColors.text_BEBEBE,
                    ),
                    Row(
                      children: [
                        Text('dishOptionsQuality'.tr,
                            style: TextStyle(
                                fontSize: 26.sp,
                                color: AppColors.textInBottomSheet)),
                        const Spacer(),
                        CountNumberButton(
                          initialValue: 1,
                          minValue: 1,
                          maxValue: 9999,
                          width: 158.w,
                          height: 46.w,
                          maxLength: 4,
                          numberEditable: false,
                          colorActive: AppColors.countNumberButtonActive,
                          colorInactive: AppColors.countNumberButtonInactive,
                          colorBg: AppColors.countNumberBg,
                          textStyle: TextStyle(
                              fontSize: 25.sp,
                              color: AppColors.textInBottomSheet,
                              fontWeight: AppStyle.semiBold),
                          onChanged: (value, isAdd) {
                            //TODO
                            // Handle counter value changes
                            // controller.incrementQuantity(value.toInt());
                          },
                        )
                      ],
                    ).paddingSymmetric(vertical: 8.w),
                    const DottedLine(
                      dashColor: AppColors.text_BEBEBE,
                    ),

                    // 选项列表
                    buildOptionsWidget(),

                    SizedBox(height: 30.w),
                  ],
                ).scrollable().expanded(),
                SizedBox(height: 30.w),
                // 底部确认按钮
                Align(
                  alignment: Alignment.centerRight,
                  child: ElevatedButton(
                    onPressed: () {
                      //TODO
                      // controller.confirmDishOption();
                    },
                    style: ElevatedButton.styleFrom(
                      shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.all(Radius.circular(9.r)),
                      ),
                      backgroundColor: AppColors.bgTab,
                      foregroundColor: AppColors.bgTab.withOpacity(0.5),
                    ),
                    child: Text('dishOptionsConfirm'.trArgs(['232323']),
                        style: TextStyle(
                            fontWeight: AppStyle.semiBold,
                            color: Colors.white,
                            fontSize: 22.sp)),
                  ).size(height: 69.w),
                ),
              ],
            ),
          ).commonDialog(onCloseTap: () {
            if (Get.isDialogOpen == true) {
              Get.back();
            }
          });
        });
  }

  Widget buildOptionsWidget() {
    var optionsByGroup = controller.getOptionListByGroup(data.spec.options);

    return Column(
      mainAxisSize: MainAxisSize.min,
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        // 选项 and 其他
        ...optionsByGroup.map((entry) {
          var key = entry.key;
          var value = entry.value;
          var isGroup = key.isNotEmpty;
          var title = isGroup ? key : 'dishOptionsOthers'.tr;
          var specList = isGroup ? value[0].options : value;
          return OptionSelector(groupId: key, title: title, specList: specList)
              .paddingOnly(top: 20.w);
        })
      ],
    );
  }
}
